﻿//************************************************************************************************************
// CoNatural.Threading.dll
// Author: Roger Torres
//************************************************************************************************************
// You can use, modify, and distribute the source code and executable programs based on the source code.
// This source code is provided "as is" and without warranties as to performance or merchantability. 
// The author and/or distributors of this source code may have made statements about this source code. 
// Any such statements do not constitute warranties and shall not be relied on by the user in deciding 
// whether to use this source code. This source code is provided without any express or implied warranties 
// whatsoever. Because of the diversity of conditions and hardware under which this source code may be used, 
// no warranty of fitness for a particular purpose is offered. The user is advised to test the source code 
// thoroughly before relying on it. The user must assume the entire risk of using the source code.
//************************************************************************************************************

using System;

namespace CoNatural.Threading.Async {
   /// <summary>
   /// Wraps operation messages, used mainly for debugging. 
   /// Raises trace events on the source async action at runtime.
   /// </summary>
   public class Trace : AsyncAction {
      /// <summary>
      /// The trace message
      /// </summary>
      public string Message { get; private set; }

      /// <summary>
      /// Constructor. These actions don't have a name or raise events.
      /// </summary>
      /// <param name="message">The trace message.</param>
      public Trace(string message)
         : base(string.Empty, false) {
         Message = message;
      }

      /// <summary>
      /// Invokes the Trace method on the source async action and continues the operation.
      /// </summary>
      /// <param name="source">The source async action.</param>
      /// <param name="nextAction">The delegate to continue.</param>
      internal override void BeginInvoke(AsyncAction source, Action nextAction) {
         source.Trace(Message);
         nextAction();
      }
   }
}
